perm filename GRAD.PAL[SYS,HE] blob sn#126200 filedate 1975-02-07 generic text, type C, neo UTF8
COMMENT ⊗   VALID 00004 PAGES
C REC  PAGE   DESCRIPTION
C00001 00001
C00002 00002	.SBTTL		Sobel gradient operator
C00004 00003	
C00006 00004	
C00009 ENDMK
C⊗;
.SBTTL		Sobel gradient operator

; assumes 4 bit samples packed 2/byte - does not test limits
; OLD points to a data block as follows:

SPTR=0		; points to word at upper left corner of 3x3 operator
SBYT=2		; 0-3 to indicate position in word of 1st sample
SLENG=4		; number of bytes/line in picture (must be even)
SMAG=6		; magnitude indicator stored here at exit (range 0-15)
		; calculated as (magnitude↑2+127)/128
SDX=10		; X component of vector here at exit (0 to +-60)
SDY=12		; Y   "
SDIR=14		; direction number here at exit (0-8 in low byte

GRAD:	MOV #3,CNT		; unpack samples
	MOV #GRID,NEW
	MOV SPTR(OLD),C
	MOV SBYT(OLD),LENG	;	calculate byte offset
	ASL LENG
	MOV DISPT(LENG),IND
SOBL3:	MOV (C),A		;	pick up first word
	MOV #3,LENG		;	store 3 samples in this line
	JMP @IND		;	align samples correctly

BYT1:	ASHC #-8.,A
	BR SOBL2

BYT2:	MOV 2(C),B
	ASHC #-4,A
	BR SOBL2

BYT3:	MOV 2(C),B
	BR SOBL2

BYT0:	MOV A,B

SOBL1:	ASHC #4,A		
SOBL2:	BIC #177760,A
	MOV A,(NEW)+
	SOB LENG,SOBL1
	ADD SLENG(OLD),C	;	increment for next line
	DEC CNT
	BNE SOBL3
	SUB A11,A33		; generate DX and DY
	SUB A21,A23
	SUB A31,A13
	SUB A32,A12
	ASL A23
	ASL A12

	ADD A13,DX
	ADD A33,DX
	ADD A13,DY
	SUB A33,DY
	MOV DX,B		; generate magnitude indicator
	MUL B,B
	MOV DY,LENG
	MUL LENG,LENG
	ADD LENG,B
	ADD #127.,B
	ASH #-7,B
	MOV B,SMAG(OLD)
	BEQ NODIR
	MOV DX,SDX(OLD)
	MOV DY,SDY(OLD)
	MOV DY,B		; generate direction number from table
	ASH #-2,B
	ADD #15.,B
	MOV DX,A
	ASH #-2,A
	ADD #15.,A
	MUL #31.,B
	ADD A,B
	MOVB DIR(B),SDIR(OLD)
NODIR:	RTS PC

CNT:	0
IND:	0			; address of byte dispatch

GRID:				; holds 9 samples after unpacking
A11:	0			; stored left to right and top to bottom
DY:
A12:	0
A13:	0
A21:	0
A22:	0
DX:
A23:	0
A31:	0
A32:	0
A33:	0


DISPT:	BYT0			; byte dispatch table
	BYT1
	BYT2
	BYT3

.MACR	OCT A
  .IRPC	FOO,A
  .BYTE	FOO
  .ENDM
.ENDM

DIR:	OCT 5555555554444444444443333333333
	OCT 5555555554444444444443333333333
	OCT 5555555555444444444443333333333
	OCT 5555555555444444444433333333333
	OCT 5555555555544444444433333333333
	OCT 5555555555544444444333333333333
	OCT 5555555555554444444333333333333
	OCT 5555555555554444444333333333333
	OCT 5555555555555444443333333333333
	OCT 6655555555555444443333333333322
	OCT 6666555555555444433333333332222
	OCT 6666666555555544433333332222222
	OCT 6666666665555544333333222222222
	OCT 6666666666655554333222222222222
	OCT 6666666666666654322222222222222
	OCT 6666666666666660222222222222222
	OCT 6666666666667770111222222222222
	OCT 6666666666777770111112222222222
	OCT 6666666777777700011111112222222
	OCT 6666677777777700011111111122222
	OCT 6667777777777000001111111111222
	OCT 7777777777777000001111111111111
	OCT 7777777777777000001111111111111
	OCT 7777777777770000000111111111111
	OCT 7777777777770000000111111111111
	OCT 7777777777700000000011111111111
	OCT 7777777777700000000011111111111
	OCT 7777777777700000000011111111111
	OCT 7777777777000000000001111111111
	OCT 7777777777000000000001111111111
	OCT 7777777770000000000000111111111

.EVEN